home *** CD-ROM | disk | FTP | other *** search
/ The Very Best of Atari Inside / The Very Best of Atari Inside 1.iso / mint / mntlb20 / lib / access.c < prev    next >
C/C++ Source or Header  |  1991-11-10  |  908b  |  46 lines

  1. /* access() emulation; relies heavily on stat() */
  2.  
  3. #include <types.h>
  4. #include <stat.h>
  5. #include <fcntl.h>
  6. #include <errno.h>
  7. #include <unistd.h>
  8.  
  9. extern int __mint;
  10.  
  11. int
  12. access(path, mode)
  13.     const char *path;
  14.     int mode;
  15. {
  16.     struct stat sb;
  17.     int uid, gid;
  18.  
  19.     if (stat(path, &sb) < 0)
  20.         return -1;    /* errno was set by stat() */
  21.     if (mode == F_OK)
  22.         return 0;    /* existence test succeeded */
  23.  
  24. /* somewhat crufty code -- relies on R_OK, etc. matching the bits in the
  25.    file mode, but what the heck, we can do this
  26.  */
  27.     if (__mint < 9 || ( (uid = geteuid()) == sb.st_uid ) ) {
  28.         if ( ((sb.st_mode >> 6) & mode) == mode )
  29.             return 0;
  30.         else
  31.             goto accdn;
  32.     }
  33.  
  34.     if ( (gid = getegid()) == sb.st_gid ) {
  35.         if ( ((sb.st_mode >> 3) & mode) == mode )
  36.             return 0;
  37.         else
  38.             goto accdn;
  39.     }
  40.  
  41.     if ( (sb.st_mode & mode) == mode)
  42.         return 0;
  43. accdn:
  44.     errno = EACCESS; return -1;
  45. }
  46.